package com.aaa.mrcai.service.impl;

import com.aaa.mrcai.mapper.StoreMapper;
import com.aaa.mrcai.pojo.entity.*;
import com.aaa.mrcai.pojo.vo.OtherVO;
import com.aaa.mrcai.query.StoreQuery;
import com.aaa.mrcai.result.AjaxResult;
import com.aaa.mrcai.service.StoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import javax.servlet.http.HttpSession;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * TODO
 *
 * @author 惠普
 * @date 2021/12/6  22:06
 */
@Service
public class StoreServiceImpl implements StoreService {

    @Autowired
    private StoreMapper storeMapper;

    @Autowired
    private HttpSession session;

    @Override
    public AjaxResult listStore(StoreQuery query) {
        //当前页信息
        List<Store> stores = storeMapper.listStore(query);
        //符合条件总数量，用于分页
        Long totalCount = storeMapper.getTotalCount(query);
        Map<String, Object> map = new HashMap<>();
        map.put("pageData",stores);
        map.put("totalCount",totalCount);
        return AjaxResult.success(map);
    }

    @Override
    public AjaxResult updateState(StoreQuery query) {
        int i = storeMapper.updateState(query);
        if(i>0){
            return AjaxResult.success();
        }else {
            return AjaxResult.defeated();
        }
    }

    @Override
    public AjaxResult listState() {
        List<State> states = storeMapper.listState();
        if(states.size()>0){
            return AjaxResult.success(states);
        }else {
            return AjaxResult.defeated();
        }
    }

    @Override
    public AjaxResult getStoreDetail(int id) {
        StoreDetail storeDetail = storeMapper.getStoreDetail(id);
        System.out.println(storeDetail);
        if(storeDetail!=null){
            return AjaxResult.success(storeDetail);
        }else {
            return AjaxResult.defeated();
        }
    }

    @Override
    public AjaxResult addOther(OtherVO otherVO) {
        EmpInfo emp = (EmpInfo) session.getAttribute("emp");
        System.out.println(emp);
        LocalDate date = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        otherVO.setCheckdate(date.format(formatter));
        otherVO.setCheckman(emp.getEname());
        int i = storeMapper.addOther(otherVO);
        if (i>0){
            return AjaxResult.success();
        }
        return AjaxResult.defeated();
    }
}
